﻿package Installer::Command::MakeFiles;

use JIP::ToolSet; # load my commonly-used modules in a single import
use parent qw( Build::Command );

use Taxonomy qw(
    getAllTaxonomies
    contentTypeToTableName
    contentTypeToTableType
);

sub execute {
    my $self = shift;

    $self->log->Note('executing "MakeFiles" command...');

    foreach my $eachContentType ( getAllTaxonomies() ) {
        my $tableName = contentTypeToTableName( $eachContentType );

        if ( contentTypeToTableType( $eachContentType ) eq 'file' ) {
            $self->log->Note( "create '". $tableName ."' as 'file' table" );

            $self->orm->storage->dbh->do(
                "CREATE TABLE `$tableName`
                 (
                     hostId    smallint     unsigned  NOT NULL,
                     fileName  varchar(255)           NOT NULL,
                     filePath  varchar(255)           NOT NULL,
                     fileSize  bigint       unsigned  NOT NULL
                 )
                 ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC"
            );
        }
        elsif ( contentTypeToTableType( $eachContentType ) eq 'dir' ) {
            $self->log->Note( "create '". $tableName ."' as 'dir' table" );

            $self->orm->storage->dbh->do(
                "CREATE TABLE `$tableName`
                 (
                     hostId       smallint     unsigned  NOT NULL,
                     folderName   varchar(255)           NOT NULL,
                     folderPath   varchar(255)           NOT NULL
                 )
                 ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC"
            );
        }
    }

    $self->log->Note('"MakeFiles" finished');
}

1;
